package com.ocom.mqsimple.business.unicom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.mqsimple.config.UnicomMqConfig;
import com.ocom.mqsimple.feign.ExposeClientService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;


@Component
public class UnicomConsumerMqListener {

    private Logger logger = LoggerFactory.getLogger(UnicomConsumerMqListener.class);

    @Autowired
    ExposeClientService exposeClientService;

    @RabbitListener(queues = UnicomMqConfig.UNICOM_QUEUE)
    public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
        logger.info("message【{}】", message);
        String requestMessage = new String(message.getBody(), StandardCharsets.UTF_8);

        try {
            JSONObject jsonObject = JSON.parseObject(requestMessage);
            exposeClientService.unicomConsEvent(jsonObject.getLong("comId"), jsonObject.getString("orderNo"));
        } catch (Exception e) {
            logger.info("Unicom-出现异常队列消息开始进入死信队列{{}}", e);
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 把该监听命名为B监听
     * 监听死信队列  远程服务支付接口发送消息  该队列会接受
     * [message, headers, channel]
     *
     * @return {@link }
     * @throws
     */
//    @RabbitListener(queues = UnicomMqConfig.UNICOM_DIE_QUEUE)
//    public void processForDie(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
//        //死信队列放弃
//        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
//        logger.info("KCMZ-死信队列放弃 ， 【{}】", message);
//    }


}
